package cn.jd.basic.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

public class SendEmail {

    @Autowired
    private static JavaMailSenderImpl sender;
    public static void send(String setemail,String section,String text,String outEmail) throws MessagingException {
        /*JavaMailSenderImpl sender = new JavaMailSenderImpl();*/
        // 1.创建复杂邮件对象
        MimeMessage mimeMessage = sender.createMimeMessage();
        // 2.发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        // 发件人
        helper.setFrom(setemail);
        System.out.println(setemail);
        // 发件主题
        helper.setSubject(section);
        System.out.println(section);
        // 3.设置文本内容，第二个参数 true 代表是否是一个html内容 texe发件内容
        System.out.println(text);
        helper.setText("<h1>"+text+"</h1>"+
                "<img src='http://dfs.java.itsource.cn/group1/M00/00/05/oYYBAGKDLDuADv2ZAABvwky9b5015.jpeg\"' />",true);
       /* // 4.添加附件  name发件附件名
        helper.addAttachment(name,new File("C:\\Users\\lenovo\\Desktop\\1.jpg"));*/
        // 5.设置收件人 收件人地址outEmail
        helper.setTo(outEmail);
        System.out.println(outEmail);
        // 6.发送邮件
        System.out.println(mimeMessage);
        sender.send(mimeMessage);
    }
}
